import os
import subprocess
import platform

def batch_execute(bag_paths, script1, script2):
    """
    Batch execute two scripts for each bag file.

    Parameters:
        bag_paths (list): List of bag file paths.
        script1 (str): First script to execute (e.g., test_read_and_save.py).
        script2 (str): Second script to execute (e.g., test_plot_heading_angle.py).
    """
    for bag_path in bag_paths:
        # Ensure the bag file exists
        if not os.path.exists(bag_path):
            print(f"Bag file not found: {bag_path}")
            continue
        
        # Derive the corresponding JSON folder path
        

        # Execute the first script


        # Execute the second script
        

if __name__ == "__main__":
    # List of bag file paths to process

    # Script paths
    script1 = "./rosbag_processor/scripts/test_read_and_save.py"
    if platform.system() == "Linux":
        path_prefix = "/media/ljn"
    elif platform.system() == "Darwin":  # Darwin 是 macOS 的系统名称
        path_prefix = "/Volumes"
    bag_files = [
        path_prefix + "/Johny_SLAM/MBZIRC2023/Finals/experiments/tuav6_finals_data/bags/uwb_heading_2024-01-21-20-09-30.bag",
        # path_prefix + "/Johny_SLAM/MBZIRC2023/Finals/experiments/tuav6_finals_data/bags/uwb_heading_2024-01-21-20-14-42.bag",
        # path_prefix + "/Johny_SLAM/MBZIRC2023/Finals/experiments/tuav6_finals_data/bags/uwb_heading_2024-01-21-20-15-07.bag",
        path_prefix + "/Johny_SLAM/MBZIRC2023/Finals/experiments/tuav6_finals_data/bags/uwb_heading_2024-01-21-20-16-51.bag",
        path_prefix + "/Johny_SLAM/MBZIRC2023/Finals/experiments/tuav6_finals_data/bags/uwb_heading_2024-01-21-20-42-30.bag",
        path_prefix + "/Johny_SLAM/MBZIRC2023/Finals/experiments/tuav6_finals_data/bags/uwb_heading_2024-01-21-21-08-50.bag",
        path_prefix + "/Johny_SLAM/MBZIRC2023/Finals/experiments/tuav6_finals_data/bags/uwb_heading_2024-01-21-21-13-18.bag",
        # path_prefix + "/Johny_SLAM/MBZIRC2023/Finals/experiments/tuav6_finals_data/bags/uwb_heading_2024-02-04-10-34-10.bag"
    ]
    # for bag_path in bag_files:
    #     print(f"Executing: {script1} with bag_path: {bag_path}")
    #     subprocess.run(["python", script1, "--bag_path", bag_path])

    # json_folders = []
    # for bag_path in bag_files:
    #     json_folder = os.path.splitext(bag_path)[0]
    #     json_folders.append(json_folder)
    json_folders = ["/Volumes/Johny_SLAM/MBZIRC2023/Finals/experiments/tuav_data_ana/uwb_heading_2024-01-25-10-19-57",
    "/Volumes/Johny_SLAM/MBZIRC2023/Finals/experiments/tuav_data_ana/uwb_heading_2024-01-23-17-42-17",
    "/Volumes/Johny_SLAM/MBZIRC2023/Finals/experiments/tuav_data_ana/uwb_heading_2024-01-18-13-07-19", 
    "/Volumes/Johny_SLAM/MBZIRC2023/Finals/experiments/tuav_data_ana/uwb_heading_2024-01-23-16-19-31"]

    # script2 = "test_plot_heading_angle.py"
    script2 = "test_plot_xyz.py"
    for json_folder in json_folders:
        print(f"Executing: {script2} with json_folder: {json_folder}")
        subprocess.run(["python3", script2, "--json_folder", json_folder])
    
